//开窗口,普通弹出窗口
function openWindow(this_url, w_width, w_height, w_name, w_top, w_left) {
	if (!w_top) {
		w_top = (screen.height - w_height) / 2;
	}
	if (!w_left) {
		w_left = (screen.width - w_width) / 2;
	}
	w_param = "top=#t#,left=#l#,width=#w#,height=#h#,channelmode=no,directories=no,resizable=yes,location=no,menubar=no,scrollbars=yes,status=yes,titlebar=no,toolbar=no";
	w_param = w_param.replace("#h#", w_height);
	w_param = w_param.replace("#w#", w_width);
	w_param = w_param.replace("#t#", w_top);
	w_param = w_param.replace("#l#", w_left);
	if (w_name) {
		childWindow = window.open(this_url, w_name, w_param);
	} else {
		childWindow = window.open(this_url, "", w_param);
	}
}

//开模式对话框
function openDialog(this_url, w_width, w_height, w_top, w_left) {
	if (!w_top) {
		w_top = (screen.height - w_height) / 2;
	}
	if (!w_left) {
		w_left = (screen.width - w_width) / 2;
	}
	w_param = "dialogTop:#t#px;dialogLeft=#l#px;dialogWidth=#w#px;dialogHeight=#h#px;center=no;help=no;resizable=no;status=no;";
	w_param = w_param.replace("#h#", w_height);
	w_param = w_param.replace("#w#", w_width);
	w_param = w_param.replace("#t#", w_top);
	w_param = w_param.replace("#l#", w_left);
	
	window.showModalDialog(this_url, window, w_param);
}


//构造动态form,并用post的方式提交
function buildFormPost(url, target, name) {
	var urls = url.splitFirst("?");
	if (urls[1]) {
		var params = urls[1].split("&");
	}
	var formObj = document.createElement("FORM");
	formObj.action = urls[0];
	formObj.target = target;
	formObj.method = "POST";
	if (name) {
		formObj.name = name;
	}
	if (params) {
		for (var i = 0; i < params.length; i++) {
			var inputObj = document.createElement("INPUT");
			inputObj.type = "hidden";
			inputObj.name = params[i].split("=")[0];
			inputObj.value = params[i].split("=")[1];
			formObj.appendChild(inputObj);
		}
	}
	document.body.appendChild(formObj);
	formObj.submit();
	document.body.removeChild(formObj);
}

function buildFormSubmit(window, url, method, target, name) {
	var urls = url.splitFirst("?");
	if (urls[1]) {
		var params = urls[1].split("&");
	}
	var formObj = document.createElement("FORM");
	formObj.action = urls[0];
	formObj.target = target;
	//window.name = target;
	formObj.method = method;
	if (name) {
		formObj.name = name;
	}
	if (params) {
		for (var i = 0; i < params.length; i++) {
			var inputObj = document.createElement("INPUT");
			inputObj.type = "hidden";
			inputObj.name = params[i].split("=")[0];
			inputObj.value = params[i].split("=")[1];
			formObj.appendChild(inputObj);
		}
	}
	document.body.appendChild(formObj);
	formObj.submit();
	document.body.removeChild(formObj);
}

//点击表头的checkbox，全选或全不选该列checkbox
function checkboxSelectedAll(headObj) {
	checkboxName = headObj.name;
	checkboxValue = headObj.checked;
		
	//根据checkbox名字来寻找控件
	if (checkboxName.substring(0, 4) == "all_") {
		checkboxName = checkboxName.substring(4);
	} 
		
	var checkObj = document.getElementsByName(checkboxName);
	if (checkObj) {
		for (var i = 0; i < checkObj.length; i++) {
			checkObj[i].checked = checkboxValue;
		}
	}
}

//获取被选中的checkbox的项，组合成url参数返回
function getCheckboxSelected(checkboxName) {
	var ret = "";
	var checkObj = document.getElementsByName(checkboxName);
	if (checkObj) {
		for (var i = 0; i < checkObj.length; i++) {
			if (checkObj[i].checked) ret += "&" + checkboxName + "=" + checkObj[i].value;
		}
	}
	return ret;
}

//高级，隐藏部分查询条件
//初始化高级的查询条件，传入列表对应的ID
function showHiddenRow(objId, displayRows) {
	var tableObj = document.getElementById(objId);
	for (var i = 0; i < tableObj.rows.length; i++) {
		if (i >= displayRows) {
			var r = tableObj.rows[i];
			if (r.style.visibility == "visible" || r.style.visibility == "") {
				r.style.visibility = "hidden";
				r.style.display = "none";
			} else {
				r.style.visibility = "visible";
				r.style.display = "block";
			}
		}
	}	
}

function Request(findURL) {
	this.reqParam = {};
	this.reqAttr = {};
	
	this.pathname = location.pathname;
	//获取参数字符串
	var paramStr = null, attrStr = null;
	if (isValid(window.globeRequestParameter))
		paramStr = window.globeRequestParameter;
	else if (findURL)
		paramStr = location.search;
		
	if (isValid(window.globeRequestAttribute))
		attrStr = window.globeRequestAttribute;	
	
	
	
	if (paramStr != null) {
		var params = paramStr.split("&");
		for (var i = 0; i < params.length; i ++) {
			var t = params[i].split("=");
			//如果通过=切割有效
			if (t.length == 2 && isValid(t[0]) && isValid(t[1])) {
				var key = t[0].replace("&amp;", "&").replace("&el;", "=");
				var value = t[1].replace("&amp;", "&").replace("&el;", "=").replace("\\n", "\n").replace("\\r", "\r");
				this.setParameter(key, value);
			}
		}
	}
	
	if (attrStr != null) {
		var attrs = attrStr.split("&");
		for (var i = 0; i < attrs.length; i ++) {
			var t = attrs[i].split("=");
			//如果通过=切割有效
			if (t.length == 2 && isValid(t[0]) && isValid(t[1])) {
				var key = t[0].replace("&amp;", "&").replace("&el;", "=");
				var value = t[1].replace("&amp;", "&").replace("&el;", "=");
				this.setAttribute(key, value);
			}
		}
	}
}

Request.prototype.setParameter = function (key, value) {
	//如果已经有值,数组+1
	if (isValid(this.reqParam[key])) {
		var o = this.reqParam[key];
		o[o.length] = value;
		this.reqParam[key] = o;
	//没值放入一个数组
	} else {
		this.reqParam[key] = [value];
	}
}
Request.prototype.setParameters = function (key, value) {
	if (typeof value == "string")
		this.reqParam[key] = [value];
	else if (typeof value == "number")
		this.reqParam[key] = [value];
	else if (typeof value == "object" && value.length)
		this.reqParam[key] = value;
	else
		this.reqParam[key] = [value];
}
Request.prototype.getParameter = function (key) {
	if (!isValid(key)) return null;
	if (isValid(this.reqParam[key]))
		return this.reqParam[key][0];
	else
		return null;
}
Request.prototype.getParameters = function (key) {
	if (!isValid(key)) return null;
	if (isValid(this.reqParam[key]))
		return this.reqParam[key];
	else
		return null;
}
Request.prototype.removeParameter = function (key) {
	if (!isValid(key)) return null;
	if (isValid(this.reqParam[key])) {
		var o = this.reqParam[key];
		delete this.reqParam[key];
		return o;
	} else 
		return null;
}
Request.prototype.getParameterNames = function () {
	return keyOfHash(this.reqParam);
}
Request.prototype.setAttribute = function (key, value) {
	this.reqAttr[key] = value;
}

Request.prototype.getAttribute = function (key) {
	if (!isValid(key)) return null;
	if (isValid(this.reqAttr[key]))
		return this.reqAttr[key]
	else
		return null;
}
Request.prototype.removeAttribute = function (key) {
	if (!isValid(key)) return null;
	if (isValid(this.reqAttr[key])) {
		var o = this.reqAttr[key];
		delete this.reqAttr[key];
		return o;
	} else 
		return null;
}
Request.prototype.toQueryString = function () {
	return "1=1" + hashToParam(this.reqParam);
}
Request.prototype.toURL = function () {
	return this.pathname + "?1=1" + hashToParam(this.reqParam);
}
Request.prototype.clone = function () {
	return new Request();
}
Request.prototype.reload = function (method, target) {
	buildFormSubmit(window, this.toURL(), method?method:"POST", target?target:"");
}